import {StatusCode} from '../consts/StatusCode'
import {CommonConst} from '../consts/CommonConst'

export class R {
    code: number
    success: boolean
    msg: string
    data: object
    extra: object


    constructor() {
        this.code = StatusCode.Ok.valueOf()
        this.success = true
        this.msg = CommonConst.okMsg
        this.data = {}
        this.extra = {}
    }

    public static build(code: number, success: boolean, msg: string, data: object): R {
        let r = new R()
        r.code = code
        r.success = success
        r.msg = msg
        r.data = data
        return r;
    }

    public static ok(data?: object): R {
        return R.build(StatusCode.Ok.valueOf(), true, CommonConst.okMsg, data ? data : {})
    }

    public static fail(msg?: string): R {
        return R.build(StatusCode.Fail.valueOf(), false, msg ? msg : CommonConst.okMsg, {})
    }

    public static notFound() {
        return R.build(StatusCode.NotFound.valueOf(), false, CommonConst.notFound, {})
    }
}
